package com.sinoeyes.controller.system;

import com.sinoeyes.auth.core.util.SecurityUtils;
import com.sinoeyes.common.constant.UserConstants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.core.page.RestPageResult;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import com.sinoeyes.system.filter.SysConfigFilter;
import com.sinoeyes.system.service.ISysConfigService;
import com.sinoeyes.system.vo.SysConfigVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 参数配置 信息操作处理
 */
@RestController
@RequestMapping("/system/config")
@Api(value = "参数配置 API", tags = {"参数配置"})
public class SysConfigController extends BaseController
{
    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    @ApiOperation("获取参数配置列表")
    public Result<RestPageResult<SysConfigVo>> list(SysConfigFilter sysConfigFilter)
    {
        RestPageResult<SysConfigVo> pageResult = configService.selectConfigList(sysConfigFilter);
        return Result.success(pageResult);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    @ApiOperation("根据参数编号获取详细信息")
    public Result<SysConfigVo> getInfo(@PathVariable Long configId)
    {
        return Result.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/configKey/{configKey}")
    @ApiOperation("根据参数键名查询参数值")
    public Result<String> getConfigKey(@PathVariable String configKey)
    {
        return Result.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增参数配置")
    public Result<Integer> add(@RequestBody SysConfigVo config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return Result.success(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改参数配置")
    public Result<Integer> edit(@RequestBody SysConfigVo config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return Result.success(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configId}")
    @ApiOperation("删除参数配置")
    public Result<Integer> remove(@PathVariable Long configId)
    {
        return Result.success(configService.deleteConfigById(configId));
    }
}
